'use strict';

const Spinner = require('cli-spinner').Spinner

/* 
 * @Description: 检测参数类型是否是对象
 * @param: 要检测的参数
 * @return: Boolean 是/否
*/ 
function isObject(o) {
  return Object.prototype.toString.call(o)
}

/* 
 * @Description: 终端的节点选择器交互
 * @return: spinner
*/ 
function spinnerStart(string='loading...') {
  const spinner = new Spinner(`${string} %s`);
  spinner.setSpinnerString('|/-\\');
  spinner.start();
  return spinner
}

/* 
 * @Description: 时间卡顿
*/ 
function sleep(time = 1000) {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve()
    }, time)
  })
}

/* 
 * @Description: 兼容win和macOS，win上的是cp.spawn('cmd',['/c', '-e', code], {
    cwd: process.cwd(),
    stdio: 'inherit'
  })
 * @param: 命令
 * @param: 参数
 * @param: 配置项
 * @return: 兼容后的spwan命令
*/ 
function suitSpawn(command, args, options) {
  const platform = process.platform === 'win32' ? 'win32' : ''
  command = platform === 'win32' ?  'cmd' : command
  args = platform === 'win32' ? ['/c'].concat(command, args) : args
  return require('child_process').spawn(command, args, options || {})
}

module.exports = {
  isObject,
  spinnerStart,
  sleep,
  suitSpawn
};


